<?php

namespace app\seller\controller;

use app\common\controller\Seller;
use fast\Flow;
use think\Log;

/**
 * 流量卡
 *
 * @icon fa fa-circle-o
 */
class Card extends Seller
{
    
    /**
     * Card模型对象
     */
    protected $model = null;

    /**
     * @var object 参数配置
     */
    protected static $flow = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Card');
        Flow::instance(json_decode(model('Flowconf')->find(),true));
        $this->view->assign("saleList", $this->model->getSaleList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Seller.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 编辑
     */
    public function detail($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds))
        {
            if (!in_array($row[$this->dataLimitField], $adminIds))
            {
                $this->error(__('You have no permission'));
            }
        }
        if($this->request->isAjax()) {
            $result = Flow::getCardPkg($row['iccid'],$row['isp']);
            Flow::logWrite($result,LOG_PATH.$this->request->controller().DS.$this->request->action().DS);
            $data_list = [];
            if($result['code']==0)
            {
                $packages = $result['packages']['CumulRspList'];
                foreach ($packages as $pkg)
                {
                    if(strpos($pkg['OFFER_NAME'],'加餐')!==false)
                    {
                        $pkg_name = "叠加包:";

                    }
                    else
                    {
                        $pkg_name = "常规套餐:";
                    }
                    $pkg_M = intval($pkg['CUMULATION_TOTAL']/1024);
                    $pkg_value = '总共'.$pkg_M.',剩余'.sprintf("%.2fM",$pkg['CUMULATION_LEFT']/1024);
                    $data_list[] = ['name'=>$pkg_name,'value'=>$pkg_value];
                }
                $cardStatus = Flow::getCardStatus($row['iccid'],$row['isp']);
                Flow::logWrite($cardStatus,LOG_PATH.$this->request->controller().DS.$this->request->action().DS);
                if($cardStatus['code']==0)
                {
                    $data_list[] = ['name'=>'卡状态:','value'=>$cardStatus['queryRes']['productStatusName']];
                }
            }
            else
            {
                $data_list = [['value'=>'查询失败','name'=>'查询失败']];
            }
            return json($data_list);
        }
        $this->view->assign("ids", $ids);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
}
